<?php

namespace Vancexu\Tcp;

use Swoole\Thread;
use Swoole\Coroutine;

class Logger
{
    public static function info(string $message, bool $isMain = false)
    {
        // 如果是主线程 则过滤不是主线的日志
        if ($isMain && !empty(Thread::getArguments())) return;

        $backtrace = debug_backtrace();

        $message = sprintf("%s info Thread[%d] Coroutine[%d] %s [%s]",
            date('Y-m-d H:i:s'),
            Thread::getId(),
            Coroutine::getCid(),
            $message,
            ($backtrace[0]['file'] ?? "") . ':' . ($backtrace[0]['line'] ?? -1)
        );
        fwrite(STDOUT, $message . PHP_EOL);
    }
}